//
//  MegaphoneDetailViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/19.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "MegaphoneDetailViewController.h"
#import "loginTextField.h"

@interface MegaphoneDetailViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation MegaphoneDetailViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"传声筒";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self creatData];
    
   
    
}
- (void)creatData{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {//是孩子
        [dic setValue:[JHUserInfo getUserInfo].userId forKey:@"child_id"]; //孩子的id
        [dic setValue:self.child_id forKey:@"parent_id"]; //家长的id
    }else{
        [dic setValue:self.child_id forKey:@"child_id"]; //孩子的id
        [dic setValue:[JHUserInfo getUserInfo].userId forKey:@"parent_id"]; //家长的id
    }
    [dic setValue:self.sound_id forKey:@"sound_id"]; //题目的id
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppSoundGetSoundDetail] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.dataArray addObjectsFromArray: [NSMutableArray arrayWithArray:response]];
            
            UIView *view = [[UIView alloc]init];
            view.frame = CGRectMake(0, 0, kScreenW, 100);
            view.backgroundColor = [UIColor whiteColor];
            
            if (self.dataArray.count>0 && strIsEmpty(self.dataArray[0][@"answer"])) {
                CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(20, 30, kScreenW - 40, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"扔出去"];
                [completeButton addTarget:self action:@selector(throwMegaphone) forControlEvents:UIControlEventTouchUpInside];
                [view addSubview:completeButton];
            }
            self.tableView.tableFooterView = view;
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    NSDictionary *dic = self.dataArray[indexPath.row];
    UILabel *label = [[UILabel alloc]init];
    label.text = dic[@"question"];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    label.frame = CGRectMake(20, 20, kScreenW - 40, [Global getSizeOfString:dic[@"question"] maxWidth:kScreenW - 40 maxHeight:MAXFLOAT withFontSize:16].height);
    [cell.contentView addSubview:label];

    loginTextField *loginTF = [[loginTextField alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(label.frame)+5, kScreenW - 40 , 40) placeholder:@"" imageStr:@""];
    loginTF.tag = 2000 + indexPath.row;
    loginTF.text=dic[@"answer"];
    if (!strIsEmpty(dic[@"answer"])) {
        loginTF.userInteractionEnabled = NO;
    }
//    loginTF.keyboardType = UIKeyboardTypeAlphabet;
    loginTF.delegate = self;
    [cell.contentView  addSubview:loginTF];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArray[indexPath.row];
    return 20+ [Global getSizeOfString:dic[@"question"] maxWidth:kScreenW - 40 maxHeight:MAXFLOAT withFontSize:16].height + 5 + 40 + 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectZero;
    view.backgroundColor = [UIColor whiteColor];
    
//    if (self.dataArray.count>0 && strIsEmpty(self.dataArray[0][@"answer"])) {
//        CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(20, 30, kScreenW - 40, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"扔出去"];
//        [completeButton addTarget:self action:@selector(throwMegaphone) forControlEvents:UIControlEventTouchUpInside];
//        [view addSubview:completeButton];
//    }
    
    
//    for (int i = 0; i < 1; i ++) {
//        UIButton *button = [[UIButton alloc]init];
//        button.frame = CGRectMake(20 + ((kScreenW - 20*3)/2 + 20)*i, 30, (kScreenW - 20*3)/2, 50);
//        [button setTitle:i == 0?@"扔出去":@"提醒对方" forState:UIControlStateNormal];
//        button.backgroundColor = i == 0?KBACKNAVIGATIONCOLOR:[UIColor orangeColor];
//        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        button.tag = 1000+i;
//        [button addTarget:self action:@selector(throwMegaphone:) forControlEvents:UIControlEventTouchUpInside];
//        [view addSubview:button];
//    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    NSMutableDictionary *dic = self.dataArray[textField.tag - 2000];
    dic[@"answer"] = textField.text;
    [self.dataArray replaceObjectAtIndex:textField.tag - 2000 withObject:dic];
    [textField resignFirstResponder];
    return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSMutableDictionary *dic = self.dataArray[textField.tag - 2000];
    dic[@"answer"] = textField.text;
    [self.dataArray replaceObjectAtIndex:textField.tag - 2000 withObject:dic];
    [textField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
#pragma mark - 扔出去
- (void)throwMegaphone{
    [self.tableView endEditing:YES];
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {//是孩子
        [dic setValue:[JHUserInfo getUserInfo].userId forKey:@"child_id"]; //孩子的id
        [dic setValue:self.child_id forKey:@"parent_id"]; //家长的id
    }else{
        [dic setValue:self.child_id forKey:@"child_id"]; //孩子的id
        [dic setValue:[JHUserInfo getUserInfo].userId forKey:@"parent_id"]; //家长的id
    }
    [dic setValue:self.sound_id forKey:@"sound_id"]; //题目的id
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    NSData *dataFriends = [NSJSONSerialization dataWithJSONObject:self.dataArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:dataFriends encoding:NSUTF8StringEncoding];
    [dic setValue:jsonString forKey:@"answer"];
    
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppSoundAddSoundAnswer] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
//            [Global showActivityWithView:self.view withText:@""];
            if (self.throwSuccessBlack) {
                self.throwSuccessBlack();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
